﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;


namespace Simple.Tools
{
    public static class ExtensionAttribute
    {

        public static string GetClassName(this Type type)
        {
            if (type.IsDefined(typeof(ClassNameAttribute), false))
            {
                ClassNameAttribute classNameAttribute = (ClassNameAttribute)type.GetCustomAttributes(typeof(ClassNameAttribute),false)[0];
                return classNameAttribute.GetName();
            }
            else
            {
                return type.Name;
            }
        }

        public static string GetColName(this PropertyInfo prop)
        {
            if (prop.IsDefined(typeof(ColNameAttribute), false))
            {
                ColNameAttribute classNameAttribute = (ColNameAttribute)prop.GetCustomAttributes(typeof(ColNameAttribute),false)[0];
                return classNameAttribute.GetName();
            }
            else
            {
                return prop.Name;
            }
        }

        public static string GetMethodName(this MethodInfo methodInfo)
        {
            if (methodInfo.IsDefined(typeof(MethodNameAttribute)))
            {
				MethodNameAttribute classNameAttribute = methodInfo.GetCustomAttribute<MethodNameAttribute>();
				return classNameAttribute.GetName();
            }
            else
            {
                return methodInfo.Name;
            }
        }


        public static IEnumerable<PropertyInfo> GetNoIgnore(this Type type)
        {
            IEnumerable<PropertyInfo> props=type.GetProperties().
                Where(t=>t.IsDefined(typeof(IgnoreAttribute),false)==false);
            if (props==null )
            {
                return null;
            }
            return props;
        }
        
        public static IEnumerable<PropertyInfo> GetNoIgnore(this Type type,Func<PropertyInfo,bool> func)
        {
            IEnumerable<PropertyInfo> props = type.GetProperties().
                Where(t=>func.Invoke(t));
            if (props == null)
            {
                return null;
            }
            return props;
        }

      
    }
}
